2 use MediaWiki\MediaWikiServices
;
3 use MediaWiki\Revision\MutableRevisionRecord
;
4 use MediaWiki\Revision\RevisionStore
;
5 use MediaWiki\Revision\IncompleteRevisionException
;
6 use MediaWiki\Revision\RevisionRecord
;
7 use MediaWiki\Revision\SlotRecord
;
10 * RevisionDbTestBase contains test cases for the Revision class that have Database interactions.
15 abstract class RevisionDbTestBase
extends MediaWikiTestCase
{
18 * @var WikiPage $testPage
22 public function __construct( $name = null, array $data = [], $dataName = '' ) {
23 parent
::__construct( $name, $data, $dataName );
25 $this->tablesUsed
= array_merge( $this->tablesUsed
,
48 protected function addCoreDBData() {
49 // Blank out. This would fail with a modified schema, and we don't need it.
55 abstract protected function getMcrMigrationStage();
60 abstract protected function getMcrTablesToReset();
62 protected function setUp() {
63 $this->tablesUsed +
= $this->getMcrTablesToReset();
67 $this->mergeMwGlobalArrayValue(
71 12313 => 'Dummy_talk',
75 $this->mergeMwGlobalArrayValue(
76 'wgNamespaceContentModels',
78 12312 => DummyContentForTesting
::MODEL_ID
,
82 $this->mergeMwGlobalArrayValue(
85 DummyContentForTesting
::MODEL_ID
=> 'DummyContentHandlerForTesting',
86 RevisionTestModifyableContent
::MODEL_ID
=> 'RevisionTestModifyableContentHandler',
90 $this->setMwGlobals( [
91 'wgMultiContentRevisionSchemaMigrationStage' => $this->getMcrMigrationStage(),
92 'wgContentHandlerUseDB' => $this->getContentHandlerUseDB(),
93 'wgCommentTableSchemaMigrationStage' => MIGRATION_OLD
,
94 'wgActorTableSchemaMigrationStage' => SCHEMA_COMPAT_OLD
,
97 $this->overrideMwServices();
99 if ( !$this->testPage
) {
101 * We have to create a new page for each subclass as the page creation may result
102 * in different DB fields being filled based on configuration.
104 $this->testPage
= $this->createPage( __CLASS__
, __CLASS__
);
109 * @param string $model
112 protected function getMockTitle() {
113 $mock = $this->getMockBuilder( Title
::class )
114 ->disableOriginalConstructor()
116 $mock->expects( $this->any() )
117 ->method( 'getNamespace' )
118 ->will( $this->returnValue( $this->getDefaultWikitextNS() ) );
119 $mock->expects( $this->any() )
120 ->method( 'getPrefixedText' )
121 ->will( $this->returnValue( __CLASS__
) );
122 $mock->expects( $this->any() )
123 ->method( 'getDBkey' )
124 ->will( $this->returnValue( __CLASS__
) );
125 $mock->expects( $this->any() )
126 ->method( 'getArticleID' )
127 ->will( $this->returnValue( 23 ) );
132 abstract protected function getContentHandlerUseDB();
134 private function makeRevisionWithProps( $props = null ) {
135 if ( $props === null ) {
139 if ( !isset( $props['content'] ) && !isset( $props['text'] ) ) {
140 $props['text'] = 'Lorem Ipsum';
143 if ( !isset( $props['user_text'] ) ) {
144 $user = $this->getTestUser()->getUser();
145 $props['user_text'] = $user->getName();
146 $props['user'] = $user->getId();
149 if ( !isset( $props['user'] ) ) {
153 if ( !isset( $props['comment'] ) ) {
154 $props['comment'] = 'just a test';
157 if ( !isset( $props['page'] ) ) {
158 $props['page'] = $this->testPage
->getId();
161 if ( !isset( $props['content_model'] ) ) {
162 $props['content_model'] = CONTENT_MODEL_WIKITEXT
;
165 $rev = new Revision( $props );
167 $dbw = wfGetDB( DB_MASTER
);
168 $rev->insertOn( $dbw );
174 * @param string $titleString
175 * @param string $text
176 * @param string|null $model
180 private function createPage( $titleString, $text, $model = null ) {
181 if ( !preg_match( '/:/', $titleString ) &&
182 ( $model === null ||
$model === CONTENT_MODEL_WIKITEXT
)
184 $ns = $this->getDefaultWikitextNS();
185 $titleString = MWNamespace
::getCanonicalName( $ns ) . ':' . $titleString;
188 $title = Title
::newFromText( $titleString );
189 $wikipage = new WikiPage( $title );
191 // Delete the article if it already exists
192 if ( $wikipage->exists() ) {
193 $wikipage->doDeleteArticle( "done" );
196 $content = ContentHandler
::makeContent( $text, $title, $model );
197 $wikipage->doEditContent( $content, __METHOD__
, EDIT_NEW
);
202 private function assertRevEquals( Revision
$orig, Revision
$rev = null ) {
203 $this->assertNotNull( $rev, 'missing revision' );
205 $this->assertEquals( $orig->getId(), $rev->getId() );
206 $this->assertEquals( $orig->getPage(), $rev->getPage() );
207 $this->assertEquals( $orig->getTimestamp(), $rev->getTimestamp() );
208 $this->assertEquals( $orig->getUser(), $rev->getUser() );
209 $this->assertEquals( $orig->getContentModel(), $rev->getContentModel() );
210 $this->assertEquals( $orig->getContentFormat(), $rev->getContentFormat() );
211 $this->assertEquals( $orig->getSha1(), $rev->getSha1() );
215 * @covers Revision::getRecentChange
217 public function testGetRecentChange() {
218 $rev = $this->testPage
->getRevision();
219 $recentChange = $rev->getRecentChange();
221 // Make sure various attributes look right / the correct entry has been retrieved.
222 $this->assertEquals( $rev->getTimestamp(), $recentChange->getAttribute( 'rc_timestamp' ) );
224 $rev->getTitle()->getNamespace(),
225 $recentChange->getAttribute( 'rc_namespace' )
228 $rev->getTitle()->getDBkey(),
229 $recentChange->getAttribute( 'rc_title' )
231 $this->assertEquals( $rev->getUser(), $recentChange->getAttribute( 'rc_user' ) );
232 $this->assertEquals( $rev->getUserText(), $recentChange->getAttribute( 'rc_user_text' ) );
233 $this->assertEquals( $rev->getComment(), $recentChange->getAttribute( 'rc_comment' ) );
234 $this->assertEquals( $rev->getPage(), $recentChange->getAttribute( 'rc_cur_id' ) );
235 $this->assertEquals( $rev->getId(), $recentChange->getAttribute( 'rc_this_oldid' ) );
239 * @covers Revision::insertOn
241 public function testInsertOn_success() {
242 $parentId = $this->testPage
->getLatest();
244 // If an ExternalStore is set don't use it.
245 $this->setMwGlobals( 'wgDefaultExternalStore', false );
247 $rev = new Revision( [
248 'page' => $this->testPage
->getId(),
249 'title' => $this->testPage
->getTitle(),
250 'text' => 'Revision Text',
251 'comment' => 'Revision comment',
254 $revId = $rev->insertOn( wfGetDB( DB_MASTER
) );
256 $this->assertInternalType( 'integer', $revId );
257 $this->assertSame( $revId, $rev->getId() );
259 // getTextId() must be an int!
260 $this->assertInternalType( 'integer', $rev->getTextId() );
262 $mainSlot = $rev->getRevisionRecord()->getSlot( SlotRecord
::MAIN
, RevisionRecord
::RAW
);
264 // we currently only support storage in the text table
265 $textId = MediaWikiServices
::getInstance()
267 ->getTextIdFromAddress( $mainSlot->getAddress() );
271 [ 'old_id', 'old_text' ],
273 [ [ strval( $textId ), 'Revision Text' ] ]
286 "rev_id = {$rev->getId()}",
288 strval( $rev->getId() ),
289 strval( $this->testPage
->getId() ),
294 's0ngbdoxagreuf2vjtuxzwdz64n29xm',
299 public function provideInsertOn_exceptionOnIncomplete() {
300 $content = new TextContent( '' );
301 $user = User
::newFromName( 'Foo' );
303 yield
'no parent' => [
305 'content' => $content,
309 IncompleteRevisionException
::class,
310 "rev_page field must not be 0!"
313 yield
'no comment' => [
315 'content' => $content,
319 IncompleteRevisionException
::class,
320 "comment must not be NULL!"
323 yield
'no content' => [
329 IncompleteRevisionException
::class,
330 "Uninitialized field: content_address" // XXX: message may change
335 * @dataProvider provideInsertOn_exceptionOnIncomplete
336 * @covers Revision::insertOn
338 public function testInsertOn_exceptionOnIncomplete( $array, $expException, $expMessage ) {
339 // If an ExternalStore is set don't use it.
340 $this->setMwGlobals( 'wgDefaultExternalStore', false );
341 $this->setExpectedException( $expException, $expMessage );
343 $title = Title
::newFromText( 'Nonexistant-' . __METHOD__
);
344 $rev = new Revision( $array, 0, $title );
346 $rev->insertOn( wfGetDB( DB_MASTER
) );
350 * @covers Revision::newFromTitle
352 public function testNewFromTitle_withoutId() {
353 $latestRevId = $this->testPage
->getLatest();
355 $rev = Revision
::newFromTitle( $this->testPage
->getTitle() );
357 $this->assertTrue( $this->testPage
->getTitle()->equals( $rev->getTitle() ) );
358 $this->assertEquals( $latestRevId, $rev->getId() );
362 * @covers Revision::newFromTitle
364 public function testNewFromTitle_withId() {
365 $latestRevId = $this->testPage
->getLatest();
367 $rev = Revision
::newFromTitle( $this->testPage
->getTitle(), $latestRevId );
369 $this->assertTrue( $this->testPage
->getTitle()->equals( $rev->getTitle() ) );
370 $this->assertEquals( $latestRevId, $rev->getId() );
374 * @covers Revision::newFromTitle
376 public function testNewFromTitle_withBadId() {
377 $latestRevId = $this->testPage
->getLatest();
379 $rev = Revision
::newFromTitle( $this->testPage
->getTitle(), $latestRevId +
1 );
381 $this->assertNull( $rev );
385 * @covers Revision::newFromRow
387 public function testNewFromRow() {
388 $orig = $this->makeRevisionWithProps();
390 $dbr = wfGetDB( DB_REPLICA
);
391 $revQuery = Revision
::getQueryInfo();
392 $res = $dbr->select( $revQuery['tables'], $revQuery['fields'], [ 'rev_id' => $orig->getId() ],
393 __METHOD__
, [], $revQuery['joins'] );
394 $this->assertTrue( is_object( $res ), 'query failed' );
396 $row = $res->fetchObject();
399 $rev = Revision
::newFromRow( $row );
401 $this->assertRevEquals( $orig, $rev );
404 public function provideNewFromArchiveRow() {
412 return $f +
[ 'ar_namespace', 'ar_title' ];
417 unset( $f['ar_text_id'] );
423 unset( $f['ar_page_id'] );
429 unset( $f['ar_parent_id'] );
435 unset( $f['ar_rev_id'] );
441 unset( $f['ar_sha1'] );
448 * @dataProvider provideNewFromArchiveRow
449 * @covers Revision::newFromArchiveRow
451 public function testNewFromArchiveRow( $selectModifier ) {
452 $services = MediaWikiServices
::getInstance();
454 $store = new RevisionStore(
455 $services->getDBLoadBalancer(),
456 $services->getService( '_SqlBlobStore' ),
457 $services->getMainWANObjectCache(),
458 $services->getCommentStore(),
459 $services->getContentModelStore(),
460 $services->getSlotRoleStore(),
461 $this->getMcrMigrationStage(),
462 $services->getActorMigration()
465 $store->setContentHandlerUseDB( $this->getContentHandlerUseDB() );
466 $this->setService( 'RevisionStore', $store );
468 $page = $this->createPage(
469 'RevisionStorageTest_testNewFromArchiveRow',
471 CONTENT_MODEL_WIKITEXT
473 $orig = $page->getRevision();
474 $page->doDeleteArticle( 'test Revision::newFromArchiveRow' );
476 $dbr = wfGetDB( DB_REPLICA
);
477 $arQuery = Revision
::getArchiveQueryInfo();
478 $arQuery['fields'] = $selectModifier( $arQuery['fields'] );
480 $arQuery['tables'], $arQuery['fields'], [ 'ar_rev_id' => $orig->getId() ],
481 __METHOD__
, [], $arQuery['joins']
483 $this->assertTrue( is_object( $res ), 'query failed' );
485 $row = $res->fetchObject();
488 // MCR migration note: $row is now required to contain ar_title and ar_namespace.
489 // Alternatively, a Title object can be passed to RevisionStore::newRevisionFromArchiveRow
490 $rev = Revision
::newFromArchiveRow( $row );
492 $this->assertRevEquals( $orig, $rev );
496 * @covers Revision::newFromArchiveRow
498 public function testNewFromArchiveRowOverrides() {
499 $page = $this->createPage(
500 'RevisionStorageTest_testNewFromArchiveRow',
502 CONTENT_MODEL_WIKITEXT
504 $orig = $page->getRevision();
505 $page->doDeleteArticle( 'test Revision::newFromArchiveRow' );
507 $dbr = wfGetDB( DB_REPLICA
);
508 $arQuery = Revision
::getArchiveQueryInfo();
510 $arQuery['tables'], $arQuery['fields'], [ 'ar_rev_id' => $orig->getId() ],
511 __METHOD__
, [], $arQuery['joins']
513 $this->assertTrue( is_object( $res ), 'query failed' );
515 $row = $res->fetchObject();
518 $rev = Revision
::newFromArchiveRow( $row, [ 'comment_text' => 'SOMEOVERRIDE' ] );
520 $this->assertNotEquals( $orig->getComment(), $rev->getComment() );
521 $this->assertEquals( 'SOMEOVERRIDE', $rev->getComment() );
525 * @covers Revision::newFromId
527 public function testNewFromId() {
528 $orig = $this->testPage
->getRevision();
529 $rev = Revision
::newFromId( $orig->getId() );
530 $this->assertRevEquals( $orig, $rev );
534 * @covers Revision::newFromPageId
536 public function testNewFromPageId() {
537 $rev = Revision
::newFromPageId( $this->testPage
->getId() );
538 $this->assertRevEquals(
539 $this->testPage
->getRevision(),
545 * @covers Revision::newFromPageId
547 public function testNewFromPageIdWithLatestId() {
548 $rev = Revision
::newFromPageId(
549 $this->testPage
->getId(),
550 $this->testPage
->getLatest()
552 $this->assertRevEquals(
553 $this->testPage
->getRevision(),
559 * @covers Revision::newFromPageId
561 public function testNewFromPageIdWithNotLatestId() {
562 $content = new WikitextContent( __METHOD__
);
563 $this->testPage
->doEditContent( $content, __METHOD__
);
564 $rev = Revision
::newFromPageId(
565 $this->testPage
->getId(),
566 $this->testPage
->getRevision()->getPrevious()->getId()
568 $this->assertRevEquals(
569 $this->testPage
->getRevision()->getPrevious(),
575 * @covers Revision::fetchRevision
577 public function testFetchRevision() {
578 // Hidden process cache assertion below
579 $this->testPage
->getRevision()->getId();
581 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
582 $id = $this->testPage
->getRevision()->getId();
584 $this->hideDeprecated( 'Revision::fetchRevision' );
585 $res = Revision
::fetchRevision( $this->testPage
->getTitle() );
587 # note: order is unspecified
589 while ( ( $row = $res->fetchObject() ) ) {
590 $rows[$row->rev_id
] = $row;
593 $this->assertEmpty( $rows, 'expected empty set' );
597 * @covers Revision::getPage
599 public function testGetPage() {
600 $page = $this->testPage
;
602 $orig = $this->makeRevisionWithProps( [ 'page' => $page->getId() ] );
603 $rev = Revision
::newFromId( $orig->getId() );
605 $this->assertEquals( $page->getId(), $rev->getPage() );
609 * @covers Revision::isCurrent
611 public function testIsCurrent() {
612 $rev1 = $this->testPage
->getRevision();
614 # @todo find out if this should be true
615 # $this->assertTrue( $rev1->isCurrent() );
617 $rev1x = Revision
::newFromId( $rev1->getId() );
618 $this->assertTrue( $rev1x->isCurrent() );
620 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
621 $rev2 = $this->testPage
->getRevision();
623 # @todo find out if this should be true
624 # $this->assertTrue( $rev2->isCurrent() );
626 $rev1x = Revision
::newFromId( $rev1->getId() );
627 $this->assertFalse( $rev1x->isCurrent() );
629 $rev2x = Revision
::newFromId( $rev2->getId() );
630 $this->assertTrue( $rev2x->isCurrent() );
634 * @covers Revision::getPrevious
636 public function testGetPrevious() {
637 $oldestRevision = $this->testPage
->getOldestRevision();
638 $latestRevision = $this->testPage
->getLatest();
640 $this->assertNull( $oldestRevision->getPrevious() );
642 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
643 $newRevision = $this->testPage
->getRevision();
645 $this->assertNotNull( $newRevision->getPrevious() );
646 $this->assertEquals( $latestRevision, $newRevision->getPrevious()->getId() );
650 * @covers Revision::getNext
652 public function testGetNext() {
653 $rev1 = $this->testPage
->getRevision();
655 $this->assertNull( $rev1->getNext() );
657 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
658 $rev2 = $this->testPage
->getRevision();
660 $this->assertNotNull( $rev1->getNext() );
661 $this->assertEquals( $rev2->getId(), $rev1->getNext()->getId() );
665 * @covers Revision::newNullRevision
667 public function testNewNullRevision() {
668 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
669 $orig = $this->testPage
->getRevision();
671 $dbw = wfGetDB( DB_MASTER
);
672 $rev = Revision
::newNullRevision( $dbw, $this->testPage
->getId(), 'a null revision', false );
674 $this->assertNotEquals( $orig->getId(), $rev->getId(),
675 'new null revision should have a different id from the original revision' );
676 $this->assertEquals( $orig->getTextId(), $rev->getTextId(),
677 'new null revision should have the same text id as the original revision' );
678 $this->assertEquals( $orig->getSha1(), $rev->getSha1(),
679 'new null revision should have the same SHA1 as the original revision' );
680 $this->assertTrue( $orig->getRevisionRecord()->hasSameContent( $rev->getRevisionRecord() ),
681 'new null revision should have the same content as the original revision' );
682 $this->assertEquals( __METHOD__
, $rev->getContent()->getNativeData() );
686 * @covers Revision::newNullRevision
688 public function testNewNullRevision_badPage() {
689 $dbw = wfGetDB( DB_MASTER
);
690 $rev = Revision
::newNullRevision( $dbw, -1, 'a null revision', false );
692 $this->assertNull( $rev );
696 * @covers Revision::insertOn
698 public function testInsertOn() {
699 $ip = '2600:387:ed7:947e:8c16:a1ad:dd34:1dd7';
701 $orig = $this->makeRevisionWithProps( [
705 // Make sure the revision was copied to ip_changes
706 $dbr = wfGetDB( DB_REPLICA
);
707 $res = $dbr->select( 'ip_changes', '*', [ 'ipc_rev_id' => $orig->getId() ] );
708 $row = $res->fetchObject();
710 $this->assertEquals( IP
::toHex( $ip ), $row->ipc_hex
);
712 $orig->getTimestamp(),
713 wfTimestamp( TS_MW
, $row->ipc_rev_timestamp
)
717 public static function provideUserWasLastToEdit() {
718 yield
'actually the last edit' => [ 3, true ];
719 yield
'not the current edit, but still by this user' => [ 2, true ];
720 yield
'edit by another user' => [ 1, false ];
721 yield
'first edit, by this user, but another user edited in the mean time' => [ 0, false ];
725 * @covers Revision::userWasLastToEdit
726 * @dataProvider provideUserWasLastToEdit
728 public function testUserWasLastToEdit( $sinceIdx, $expectedLast ) {
729 $userA = User
::newFromName( "RevisionStorageTest_userA" );
730 $userB = User
::newFromName( "RevisionStorageTest_userB" );
732 if ( $userA->getId() === 0 ) {
733 $userA = User
::createNew( $userA->getName() );
736 if ( $userB->getId() === 0 ) {
737 $userB = User
::createNew( $userB->getName() );
740 $ns = $this->getDefaultWikitextNS();
742 $dbw = wfGetDB( DB_MASTER
);
745 // create revisions -----------------------------
746 $page = WikiPage
::factory( Title
::newFromText(
747 'RevisionStorageTest_testUserWasLastToEdit', $ns ) );
748 $page->insertOn( $dbw );
750 $revisions[0] = new Revision( [
751 'page' => $page->getId(),
752 // we need the title to determine the page's default content model
753 'title' => $page->getTitle(),
754 'timestamp' => '20120101000000',
755 'user' => $userA->getId(),
757 'content_model' => CONTENT_MODEL_WIKITEXT
,
758 'comment' => 'edit zero'
760 $revisions[0]->insertOn( $dbw );
762 $revisions[1] = new Revision( [
763 'page' => $page->getId(),
764 // still need the title, because $page->getId() is 0 (there's no entry in the page table)
765 'title' => $page->getTitle(),
766 'timestamp' => '20120101000100',
767 'user' => $userA->getId(),
769 'content_model' => CONTENT_MODEL_WIKITEXT
,
770 'comment' => 'edit one'
772 $revisions[1]->insertOn( $dbw );
774 $revisions[2] = new Revision( [
775 'page' => $page->getId(),
776 'title' => $page->getTitle(),
777 'timestamp' => '20120101000200',
778 'user' => $userB->getId(),
780 'content_model' => CONTENT_MODEL_WIKITEXT
,
781 'comment' => 'edit two'
783 $revisions[2]->insertOn( $dbw );
785 $revisions[3] = new Revision( [
786 'page' => $page->getId(),
787 'title' => $page->getTitle(),
788 'timestamp' => '20120101000300',
789 'user' => $userA->getId(),
791 'content_model' => CONTENT_MODEL_WIKITEXT
,
792 'comment' => 'edit three'
794 $revisions[3]->insertOn( $dbw );
796 $revisions[4] = new Revision( [
797 'page' => $page->getId(),
798 'title' => $page->getTitle(),
799 'timestamp' => '20120101000200',
800 'user' => $userA->getId(),
802 'content_model' => CONTENT_MODEL_WIKITEXT
,
803 'comment' => 'edit four'
805 $revisions[4]->insertOn( $dbw );
807 // test it ---------------------------------
808 $since = $revisions[$sinceIdx]->getTimestamp();
810 $revQuery = Revision
::getQueryInfo();
811 $allRows = iterator_to_array( $dbw->select(
813 [ 'rev_id', 'rev_timestamp', 'rev_user' => $revQuery['fields']['rev_user'] ],
815 'rev_page' => $page->getId(),
816 //'rev_timestamp > ' . $dbw->addQuotes( $dbw->timestamp( $since ) )
819 [ 'ORDER BY' => 'rev_timestamp ASC', 'LIMIT' => 50 ],
823 $wasLast = Revision
::userWasLastToEdit( $dbw, $page->getId(), $userA->getId(), $since );
825 $this->assertEquals( $expectedLast, $wasLast );
829 * @param string $text
830 * @param string $title
831 * @param string $model
832 * @param string $format
836 private function newTestRevision( $text, $title = "Test",
837 $model = CONTENT_MODEL_WIKITEXT
, $format = null
839 if ( is_string( $title ) ) {
840 $title = Title
::newFromText( $title );
843 $content = ContentHandler
::makeContent( $text, $title, $model, $format );
851 'content' => $content,
852 'length' => $content->getSize(),
853 'comment' => "testing",
854 'minor_edit' => false,
856 'content_format' => $format,
863 public function provideGetContentModel() {
864 // NOTE: we expect the help namespace to always contain wikitext
866 [ 'hello world', 'Help:Hello', null, null, CONTENT_MODEL_WIKITEXT
],
867 [ 'hello world', 'User:hello/there.css', null, null, CONTENT_MODEL_CSS
],
868 [ serialize( 'hello world' ), 'Dummy:Hello', null, null, DummyContentForTesting
::MODEL_ID
],
873 * @dataProvider provideGetContentModel
874 * @covers Revision::getContentModel
876 public function testGetContentModel( $text, $title, $model, $format, $expectedModel ) {
877 $rev = $this->newTestRevision( $text, $title, $model, $format );
879 $this->assertEquals( $expectedModel, $rev->getContentModel() );
882 public function provideGetContentFormat() {
883 // NOTE: we expect the help namespace to always contain wikitext
885 [ 'hello world', 'Help:Hello', null, null, CONTENT_FORMAT_WIKITEXT
],
886 [ 'hello world', 'Help:Hello', CONTENT_MODEL_CSS
, null, CONTENT_FORMAT_CSS
],
887 [ 'hello world', 'User:hello/there.css', null, null, CONTENT_FORMAT_CSS
],
888 [ serialize( 'hello world' ), 'Dummy:Hello', null, null, DummyContentForTesting
::MODEL_ID
],
893 * @dataProvider provideGetContentFormat
894 * @covers Revision::getContentFormat
896 public function testGetContentFormat( $text, $title, $model, $format, $expectedFormat ) {
897 $rev = $this->newTestRevision( $text, $title, $model, $format );
899 $this->assertEquals( $expectedFormat, $rev->getContentFormat() );
902 public function provideGetContentHandler() {
903 // NOTE: we expect the help namespace to always contain wikitext
905 [ 'hello world', 'Help:Hello', null, null, WikitextContentHandler
::class ],
906 [ 'hello world', 'User:hello/there.css', null, null, CssContentHandler
::class ],
907 [ serialize( 'hello world' ), 'Dummy:Hello', null, null, DummyContentHandlerForTesting
::class ],
912 * @dataProvider provideGetContentHandler
913 * @covers Revision::getContentHandler
915 public function testGetContentHandler( $text, $title, $model, $format, $expectedClass ) {
916 $rev = $this->newTestRevision( $text, $title, $model, $format );
918 $this->assertEquals( $expectedClass, get_class( $rev->getContentHandler() ) );
921 public function provideGetContent() {
922 // NOTE: we expect the help namespace to always contain wikitext
924 [ 'hello world', 'Help:Hello', null, null, Revision
::FOR_PUBLIC
, 'hello world' ],
926 serialize( 'hello world' ),
928 DummyContentForTesting
::MODEL_ID
,
930 Revision
::FOR_PUBLIC
,
931 serialize( 'hello world' )
934 serialize( 'hello world' ),
938 Revision
::FOR_PUBLIC
,
939 serialize( 'hello world' )
945 * @dataProvider provideGetContent
946 * @covers Revision::getContent
948 public function testGetContent( $text, $title, $model, $format,
949 $audience, $expectedSerialization
951 $rev = $this->newTestRevision( $text, $title, $model, $format );
952 $content = $rev->getContent( $audience );
955 $expectedSerialization,
956 is_null( $content ) ?
null : $content->serialize( $format )
961 * @covers Revision::getContent
963 public function testGetContent_failure() {
964 $rev = new Revision( [
965 'page' => $this->testPage
->getId(),
966 'content_model' => $this->testPage
->getContentModel(),
967 'id' => 123456789, // not in the test DB
970 Wikimedia\
suppressWarnings(); // bad text_id will trigger a warning.
972 $this->assertNull( $rev->getContent(),
973 "getContent() should return null if the revision's text blob could not be loaded." );
975 // NOTE: check this twice, once for lazy initialization, and once with the cached value.
976 $this->assertNull( $rev->getContent(),
977 "getContent() should return null if the revision's text blob could not be loaded." );
979 Wikimedia\restoreWarnings
();
982 public function provideGetSize() {
984 [ "hello world.", CONTENT_MODEL_WIKITEXT
, 12 ],
985 [ serialize( "hello world." ), DummyContentForTesting
::MODEL_ID
, 12 ],
990 * @covers Revision::getSize
991 * @dataProvider provideGetSize
993 public function testGetSize( $text, $model, $expected_size ) {
994 $rev = $this->newTestRevision( $text, 'RevisionTest_testGetSize', $model );
995 $this->assertEquals( $expected_size, $rev->getSize() );
998 public function provideGetSha1() {
1000 [ "hello world.", CONTENT_MODEL_WIKITEXT
, Revision
::base36Sha1( "hello world." ) ],
1002 serialize( "hello world." ),
1003 DummyContentForTesting
::MODEL_ID
,
1004 Revision
::base36Sha1( serialize( "hello world." ) )
1010 * @covers Revision::getSha1
1011 * @dataProvider provideGetSha1
1013 public function testGetSha1( $text, $model, $expected_hash ) {
1014 $rev = $this->newTestRevision( $text, 'RevisionTest_testGetSha1', $model );
1015 $this->assertEquals( $expected_hash, $rev->getSha1() );
1019 * Tests whether $rev->getContent() returns a clone when needed.
1021 * @covers Revision::getContent
1023 public function testGetContentClone() {
1024 $content = new RevisionTestModifyableContent( "foo" );
1026 $rev = new Revision(
1030 'title' => Title
::newFromText( "testGetContentClone_dummy" ),
1032 'content' => $content,
1033 'length' => $content->getSize(),
1034 'comment' => "testing",
1035 'minor_edit' => false,
1039 /** @var RevisionTestModifyableContent $content */
1040 $content = $rev->getContent( Revision
::RAW
);
1041 $content->setText( "bar" );
1043 /** @var RevisionTestModifyableContent $content2 */
1044 $content2 = $rev->getContent( Revision
::RAW
);
1045 // content is mutable, expect clone
1046 $this->assertNotSame( $content, $content2, "expected a clone" );
1047 // clone should contain the original text
1048 $this->assertEquals( "foo", $content2->getText() );
1050 $content2->setText( "bla bla" );
1051 // clones should be independent
1052 $this->assertEquals( "bar", $content->getText() );
1056 * Tests whether $rev->getContent() returns the same object repeatedly if appropriate.
1057 * @covers Revision::getContent
1059 public function testGetContentUncloned() {
1060 $rev = $this->newTestRevision( "hello", "testGetContentUncloned_dummy", CONTENT_MODEL_WIKITEXT
);
1061 $content = $rev->getContent( Revision
::RAW
);
1062 $content2 = $rev->getContent( Revision
::RAW
);
1064 // for immutable content like wikitext, this should be the same object
1065 $this->assertSame( $content, $content2 );
1069 * @covers Revision::loadFromId
1071 public function testLoadFromId() {
1072 $rev = $this->testPage
->getRevision();
1073 $this->hideDeprecated( 'Revision::loadFromId' );
1074 $this->assertRevEquals(
1076 Revision
::loadFromId( wfGetDB( DB_MASTER
), $rev->getId() )
1081 * @covers Revision::loadFromPageId
1083 public function testLoadFromPageId() {
1084 $this->assertRevEquals(
1085 $this->testPage
->getRevision(),
1086 Revision
::loadFromPageId( wfGetDB( DB_MASTER
), $this->testPage
->getId() )
1091 * @covers Revision::loadFromPageId
1093 public function testLoadFromPageIdWithLatestRevId() {
1094 $this->assertRevEquals(
1095 $this->testPage
->getRevision(),
1096 Revision
::loadFromPageId(
1097 wfGetDB( DB_MASTER
),
1098 $this->testPage
->getId(),
1099 $this->testPage
->getLatest()
1105 * @covers Revision::loadFromPageId
1107 public function testLoadFromPageIdWithNotLatestRevId() {
1108 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
1109 $this->assertRevEquals(
1110 $this->testPage
->getRevision()->getPrevious(),
1111 Revision
::loadFromPageId(
1112 wfGetDB( DB_MASTER
),
1113 $this->testPage
->getId(),
1114 $this->testPage
->getRevision()->getPrevious()->getId()
1120 * @covers Revision::loadFromTitle
1122 public function testLoadFromTitle() {
1123 $this->assertRevEquals(
1124 $this->testPage
->getRevision(),
1125 Revision
::loadFromTitle( wfGetDB( DB_MASTER
), $this->testPage
->getTitle() )
1130 * @covers Revision::loadFromTitle
1132 public function testLoadFromTitleWithLatestRevId() {
1133 $this->assertRevEquals(
1134 $this->testPage
->getRevision(),
1135 Revision
::loadFromTitle(
1136 wfGetDB( DB_MASTER
),
1137 $this->testPage
->getTitle(),
1138 $this->testPage
->getLatest()
1144 * @covers Revision::loadFromTitle
1146 public function testLoadFromTitleWithNotLatestRevId() {
1147 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
1148 $this->assertRevEquals(
1149 $this->testPage
->getRevision()->getPrevious(),
1150 Revision
::loadFromTitle(
1151 wfGetDB( DB_MASTER
),
1152 $this->testPage
->getTitle(),
1153 $this->testPage
->getRevision()->getPrevious()->getId()
1159 * @covers Revision::loadFromTimestamp()
1161 public function testLoadFromTimestamp() {
1162 $this->assertRevEquals(
1163 $this->testPage
->getRevision(),
1164 Revision
::loadFromTimestamp(
1165 wfGetDB( DB_MASTER
),
1166 $this->testPage
->getTitle(),
1167 $this->testPage
->getRevision()->getTimestamp()
1173 * @covers Revision::getParentLengths
1175 public function testGetParentLengths_noRevIds() {
1178 Revision
::getParentLengths(
1179 wfGetDB( DB_MASTER
),
1186 * @covers Revision::getParentLengths
1188 public function testGetParentLengths_oneRevId() {
1189 $text = '831jr091jr0921kr21kr0921kjr0921j09rj1';
1190 $textLength = strlen( $text );
1192 $this->testPage
->doEditContent( new WikitextContent( $text ), __METHOD__
);
1193 $rev[1] = $this->testPage
->getLatest();
1196 [ $rev[1] => $textLength ],
1197 Revision
::getParentLengths(
1198 wfGetDB( DB_MASTER
),
1205 * @covers Revision::getParentLengths
1207 public function testGetParentLengths_multipleRevIds() {
1208 $textOne = '831jr091jr0921kr21kr0921kjr0921j09rj1';
1209 $textOneLength = strlen( $textOne );
1210 $textTwo = '831jr091jr092121j09rj1';
1211 $textTwoLength = strlen( $textTwo );
1213 $this->testPage
->doEditContent( new WikitextContent( $textOne ), __METHOD__
);
1214 $rev[1] = $this->testPage
->getLatest();
1215 $this->testPage
->doEditContent( new WikitextContent( $textTwo ), __METHOD__
);
1216 $rev[2] = $this->testPage
->getLatest();
1219 [ $rev[1] => $textOneLength, $rev[2] => $textTwoLength ],
1220 Revision
::getParentLengths(
1221 wfGetDB( DB_MASTER
),
1222 [ $rev[1], $rev[2] ]
1228 * @covers Revision::getTitle
1230 public function testGetTitle_fromExistingRevision() {
1232 $this->testPage
->getTitle()->equals(
1233 $this->testPage
->getRevision()->getTitle()
1239 * @covers Revision::getTitle
1241 public function testGetTitle_fromRevisionWhichWillLoadTheTitle() {
1242 $rev = new Revision( [ 'id' => $this->testPage
->getLatest() ] );
1244 $this->testPage
->getTitle()->equals(
1251 * @covers Revision::isMinor
1253 public function testIsMinor_true() {
1254 // Use a sysop to ensure we can mark edits as minor
1255 $sysop = $this->getTestSysop()->getUser();
1257 $this->testPage
->doEditContent(
1258 new WikitextContent( __METHOD__
),
1264 $rev = $this->testPage
->getRevision();
1266 $this->assertSame( true, $rev->isMinor() );
1270 * @covers Revision::isMinor
1272 public function testIsMinor_false() {
1273 $this->testPage
->doEditContent(
1274 new WikitextContent( __METHOD__
),
1278 $rev = $this->testPage
->getRevision();
1280 $this->assertSame( false, $rev->isMinor() );
1284 * @covers Revision::getTimestamp
1286 public function testGetTimestamp() {
1287 $testTimestamp = wfTimestampNow();
1289 $this->testPage
->doEditContent(
1290 new WikitextContent( __METHOD__
),
1293 $rev = $this->testPage
->getRevision();
1295 $this->assertInternalType( 'string', $rev->getTimestamp() );
1296 $this->assertTrue( strlen( $rev->getTimestamp() ) == strlen( 'YYYYMMDDHHMMSS' ) );
1297 $this->assertContains( substr( $testTimestamp, 0, 10 ), $rev->getTimestamp() );
1301 * @covers Revision::getUser
1302 * @covers Revision::getUserText
1304 public function testGetUserAndText() {
1305 $sysop = $this->getTestSysop()->getUser();
1307 $this->testPage
->doEditContent(
1308 new WikitextContent( __METHOD__
),
1314 $rev = $this->testPage
->getRevision();
1316 $this->assertSame( $sysop->getId(), $rev->getUser() );
1317 $this->assertSame( $sysop->getName(), $rev->getUserText() );
1321 * @covers Revision::isDeleted
1323 public function testIsDeleted_nothingDeleted() {
1324 $rev = $this->testPage
->getRevision();
1326 $this->assertSame( false, $rev->isDeleted( Revision
::DELETED_TEXT
) );
1327 $this->assertSame( false, $rev->isDeleted( Revision
::DELETED_COMMENT
) );
1328 $this->assertSame( false, $rev->isDeleted( Revision
::DELETED_RESTRICTED
) );
1329 $this->assertSame( false, $rev->isDeleted( Revision
::DELETED_USER
) );
1333 * @covers Revision::getVisibility
1335 public function testGetVisibility_nothingDeleted() {
1336 $rev = $this->testPage
->getRevision();
1338 $this->assertSame( 0, $rev->getVisibility() );
1342 * @covers Revision::getComment
1344 public function testGetComment_notDeleted() {
1345 $expectedSummary = 'goatlicious summary';
1347 $this->testPage
->doEditContent(
1348 new WikitextContent( __METHOD__
),
1351 $rev = $this->testPage
->getRevision();
1353 $this->assertSame( $expectedSummary, $rev->getComment() );
1357 * @covers Revision::isUnpatrolled
1359 public function testIsUnpatrolled_returnsRecentChangesId() {
1360 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
1361 $rev = $this->testPage
->getRevision();
1363 $this->assertGreaterThan( 0, $rev->isUnpatrolled() );
1364 $this->assertSame( $rev->getRecentChange()->getAttribute( 'rc_id' ), $rev->isUnpatrolled() );
1368 * @covers Revision::isUnpatrolled
1370 public function testIsUnpatrolled_returnsZeroIfPatrolled() {
1371 // This assumes that sysops are auto patrolled
1372 $sysop = $this->getTestSysop()->getUser();
1373 $this->testPage
->doEditContent(
1374 new WikitextContent( __METHOD__
),
1380 $rev = $this->testPage
->getRevision();
1382 $this->assertSame( 0, $rev->isUnpatrolled() );
1386 * This is a simple blanket test for all simple content getters and is methods to provide some
1387 * coverage before the split of Revision into multiple classes for MCR work.
1388 * @covers Revision::getContent
1389 * @covers Revision::getSerializedData
1390 * @covers Revision::getContentModel
1391 * @covers Revision::getContentFormat
1392 * @covers Revision::getContentHandler
1394 public function testSimpleContentGetters() {
1395 $expectedText = 'testSimpleContentGetters in Revision. Goats love MCR...';
1396 $expectedSummary = 'goatlicious testSimpleContentGetters summary';
1398 $this->testPage
->doEditContent(
1399 new WikitextContent( $expectedText ),
1402 $rev = $this->testPage
->getRevision();
1404 $this->assertSame( $expectedText, $rev->getContent()->getNativeData() );
1405 $this->assertSame( $expectedText, $rev->getSerializedData() );
1406 $this->assertSame( $this->testPage
->getContentModel(), $rev->getContentModel() );
1407 $this->assertSame( $this->testPage
->getContent()->getDefaultFormat(), $rev->getContentFormat() );
1408 $this->assertSame( $this->testPage
->getContentHandler(), $rev->getContentHandler() );
1412 * @covers Revision::newKnownCurrent
1414 public function testNewKnownCurrent() {
1415 // Setup the services
1416 $this->overrideMwServices();
1417 $cache = new WANObjectCache( [ 'cache' => new HashBagOStuff() ] );
1418 $this->setService( 'MainWANObjectCache', $cache );
1419 $db = wfGetDB( DB_MASTER
);
1421 // Get a fresh revision to use during testing
1422 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
1423 $rev = $this->testPage
->getRevision();
1425 // Clear any previous cache for the revision during creation
1426 $key = $cache->makeGlobalKey(
1427 RevisionStore
::ROW_CACHE_KEY
,
1432 $cache->delete( $key, WANObjectCache
::HOLDOFF_NONE
);
1433 $this->assertFalse( $cache->get( $key ) );
1435 // Get the new revision and make sure it is in the cache and correct
1436 $newRev = Revision
::newKnownCurrent( $db, $rev->getPage(), $rev->getId() );
1437 $this->assertRevEquals( $rev, $newRev );
1439 $cachedRow = $cache->get( $key );
1440 $this->assertNotFalse( $cachedRow );
1441 $this->assertEquals( $rev->getId(), $cachedRow->rev_id
);
1444 public function testNewKnownCurrent_withPageId() {
1445 $db = wfGetDB( DB_MASTER
);
1447 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
1448 $rev = $this->testPage
->getRevision();
1450 $pageId = $this->testPage
->getId();
1452 $newRev = Revision
::newKnownCurrent( $db, $pageId, $rev->getId() );
1453 $this->assertRevEquals( $rev, $newRev );
1456 public function testNewKnownCurrent_returnsFalseWhenTitleDoesntExist() {
1457 $db = wfGetDB( DB_MASTER
);
1459 $this->assertFalse( Revision
::newKnownCurrent( $db, 0 ) );
1462 public function provideUserCanBitfield() {
1463 yield
[ 0, 0, [], null, true ];
1464 // Bitfields match, user has no permissions
1465 yield
[ Revision
::DELETED_TEXT
, Revision
::DELETED_TEXT
, [], null, false ];
1466 yield
[ Revision
::DELETED_COMMENT
, Revision
::DELETED_COMMENT
, [], null, false ];
1467 yield
[ Revision
::DELETED_USER
, Revision
::DELETED_USER
, [], null, false ];
1468 yield
[ Revision
::DELETED_RESTRICTED
, Revision
::DELETED_RESTRICTED
, [], null, false ];
1469 // Bitfields match, user (admin) does have permissions
1470 yield
[ Revision
::DELETED_TEXT
, Revision
::DELETED_TEXT
, [ 'sysop' ], null, true ];
1471 yield
[ Revision
::DELETED_COMMENT
, Revision
::DELETED_COMMENT
, [ 'sysop' ], null, true ];
1472 yield
[ Revision
::DELETED_USER
, Revision
::DELETED_USER
, [ 'sysop' ], null, true ];
1473 // Bitfields match, user (admin) does not have permissions
1474 yield
[ Revision
::DELETED_RESTRICTED
, Revision
::DELETED_RESTRICTED
, [ 'sysop' ], null, false ];
1475 // Bitfields match, user (oversight) does have permissions
1476 yield
[ Revision
::DELETED_RESTRICTED
, Revision
::DELETED_RESTRICTED
, [ 'oversight' ], null, true ];
1477 // Check permissions using the title
1479 Revision
::DELETED_TEXT
,
1480 Revision
::DELETED_TEXT
,
1486 Revision
::DELETED_TEXT
,
1487 Revision
::DELETED_TEXT
,
1495 * @dataProvider provideUserCanBitfield
1496 * @covers Revision::userCanBitfield
1498 public function testUserCanBitfield( $bitField, $field, $userGroups, $title, $expected ) {
1499 $title = Title
::newFromText( $title );
1501 $this->setMwGlobals(
1502 'wgGroupPermissions',
1505 'deletedtext' => true,
1506 'deletedhistory' => true,
1509 'viewsuppressed' => true,
1510 'suppressrevision' => true,
1514 $user = $this->getTestUser( $userGroups )->getUser();
1518 Revision
::userCanBitfield( $bitField, $field, $user, $title )
1521 // Fallback to $wgUser
1522 $this->setMwGlobals(
1528 Revision
::userCanBitfield( $bitField, $field, null, $title )
1532 public function provideUserCan() {
1533 yield
[ 0, 0, [], true ];
1534 // Bitfields match, user has no permissions
1535 yield
[ Revision
::DELETED_TEXT
, Revision
::DELETED_TEXT
, [], false ];
1536 yield
[ Revision
::DELETED_COMMENT
, Revision
::DELETED_COMMENT
, [], false ];
1537 yield
[ Revision
::DELETED_USER
, Revision
::DELETED_USER
, [], false ];
1538 yield
[ Revision
::DELETED_RESTRICTED
, Revision
::DELETED_RESTRICTED
, [], false ];
1539 // Bitfields match, user (admin) does have permissions
1540 yield
[ Revision
::DELETED_TEXT
, Revision
::DELETED_TEXT
, [ 'sysop' ], true ];
1541 yield
[ Revision
::DELETED_COMMENT
, Revision
::DELETED_COMMENT
, [ 'sysop' ], true ];
1542 yield
[ Revision
::DELETED_USER
, Revision
::DELETED_USER
, [ 'sysop' ], true ];
1543 // Bitfields match, user (admin) does not have permissions
1544 yield
[ Revision
::DELETED_RESTRICTED
, Revision
::DELETED_RESTRICTED
, [ 'sysop' ], false ];
1545 // Bitfields match, user (oversight) does have permissions
1546 yield
[ Revision
::DELETED_RESTRICTED
, Revision
::DELETED_RESTRICTED
, [ 'oversight' ], true ];
1550 * @dataProvider provideUserCan
1551 * @covers Revision::userCan
1553 public function testUserCan( $bitField, $field, $userGroups, $expected ) {
1554 $this->setMwGlobals(
1555 'wgGroupPermissions',
1558 'deletedtext' => true,
1559 'deletedhistory' => true,
1562 'viewsuppressed' => true,
1563 'suppressrevision' => true,
1567 $user = $this->getTestUser( $userGroups )->getUser();
1568 $revision = new Revision( [ 'deleted' => $bitField ], 0, $this->testPage
->getTitle() );
1572 $revision->userCan( $field, $user )
1576 public function provideGetTextId() {
1579 $slot = new SlotRecord( (object)[
1580 'slot_revision_id' => 42,
1581 'slot_content_id' => 1,
1582 'content_address' => 'tt:789',
1583 'model_name' => CONTENT_MODEL_WIKITEXT
,
1584 'role_name' => SlotRecord
::MAIN
,
1586 ], new WikitextContent( 'Test' ) );
1588 $rec = new MutableRevisionRecord( $this->testPage
->getTitle() );
1590 $rec->setSlot( $slot );
1592 yield
[ $rec, 789 ];
1596 * @dataProvider provideGetTextId
1597 * @covers Revision::getTextId()
1599 public function testGetTextId( $spec, $expected ) {
1600 $rev = new Revision( $spec, 0, $this->testPage
->getTitle() );
1601 $this->assertSame( $expected, $rev->getTextId() );